package yogiro.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import yogiro.dao.CommentDAO;
import yogiro.to.CommentTO;
import yogiro.util.Utilities;

public class CommentService {
	private CommentDAO commentDAO;
	public CommentService(CommentDAO commentDAO){
		this.commentDAO = commentDAO;
	}
	
	
	public String replace(String content){
		content = content.replace("<", "&lt");
		content = content.replace(">", "&gt");
		content = content.replace(" ", "&nbsp");
		content = content.replace("\n", "<br>");
		return content;
	}
	
	public ArrayList<CommentTO> selectAllComment(int tourNo)throws SQLException{
		return commentDAO.selectAllComment(tourNo);
	}
	
	public CommentTO insertComment(String grade, String content, String id, int tourNo)throws SQLException{
		int commentNo = commentDAO.sequence();
		String dateTime = Utilities.getNow();
		CommentTO cto = new CommentTO(commentNo, grade, content, dateTime, commentNo, 0, 0, id, tourNo);
		int g = Integer.parseInt(grade);
		grade = "";
		for(int i=0;i<g;i++){
			grade += "★";
		}
		for(int j=0;j<5-g;j++){
			grade += "☆";
		}
		String date = Utilities.changeDayTimeFormat(dateTime);
		cto.setCommentWritedate(date);
		cto.setCommentGrade(grade);
		commentDAO.insertComment(cto);
		return cto;
	}
	
	public void insertReply(CommentTO cto)throws SQLException{
		int commentNo = commentDAO.sequence();
		cto.setCommentNo(commentNo);
		String dateTime = Utilities.getNow();
		String date = Utilities.changeDayTimeFormat(dateTime);
		cto.setCommentWritedate(date);
		commentDAO.insertComment(cto);
	}

	
	public void deleteComment(int commentNo, int tourNo) throws SQLException{
		commentDAO.deleteComment(commentNo, tourNo);
	}
	
	public CommentTO selectByNo(int commentNo) throws SQLException{
		CommentTO cto = commentDAO.selectByNo(commentNo);
		return cto;
	}
	
	public void updateCommentReply(int refamily,int restep) throws SQLException{
		commentDAO.updateCommentReply(refamily, restep);
	}
}
